const app = getApp()
var lanya = require("../../utils/BLELocker.js");
const util = require('../../utils/util.js')
var blueConStatHandler = require("../../utils/blueConStatHandler.js");
const lbbService = require('../service/service.js')

var openLockThat = null;
var openLockMacId = null;
function openLock(macId,that) {
  openLockThat = that;
  openLockMacId = macId;
  wx.showLoading({
    title: '连接中...',
    mask: true
  })
 
  lanya.connect(
    'DK' + macId,
    function (status, data) {
      blueConStatHandler.handleStatus(status, data, that, conCallBack);
    }, 0, false, null, null);
  //记录桩的服务信息 
  lanya.setCollectionServiceData(function (uuid, fce7, fce8, fce9) {
    //暂不记录空方法，保证运行
  })
}

function conCallBack() {
  let that = openLockThat  
  if (that.data.bleData) {
    lbbService.openLock({
        rfId: that.data.bleData.rfid,
        mac: openLockMacId,
        costTime: that.data.bleData.costTime,
      },function(res){
        send85Command()
      })
  } else {
    wx.showToast({
      title: '连接失败',
      image: '/images/gantan.png'
    })
  }
}

function send85Command() {
  var that = openLockThat

  lanya.send85Command(function (isSuccess) {
    if (isSuccess == false) {
      that.setData({
        showConnectStatus: false,
        openLockLeaseOptStatus: true,
        alertBoxStatus: true,
        alertBoxTipContent: "网络异常,请重新扫码",
        alertBoxBtnName: "确 定"
      })
    } else {
      wx.showToast({
        title: '开锁成功',
      });
    }
  })

}

module.exports.openLock = openLock